<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">

      <el-form-item label="门牌号" prop="houseNum" >
        <el-input v-model="dataForm.houseNum" placeholder="门牌号" disabled></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="realName">
        <el-input v-model="dataForm.realName" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item label="人员类型：" prop="personType">
        <el-radio-group v-model="dataForm.personType">
          <el-radio  label="0">住户</el-radio>
          <el-radio label="1">租户</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item prop="isMaster" :label="dataForm.personType === '0' ? '是否户主' : '是否承租人'" >
        <el-switch v-model="dataForm.isMaster" :active-value="1" :inactive-value="0" active-text="是" inactive-text="否" @change="isMasterChange" />
      </el-form-item>

      <el-form-item  :label="dataForm.personType === '0' ? '与户主关系' : '与承租人关系'" label="与户主关系" prop="relationship">
        <el-input v-model="dataForm.relationship"></el-input>
      </el-form-item>

      <el-form-item prop="gender" :label="$t('user.gender')">
        <ren-radio-group v-model="dataForm.sex" dict-type="gender"></ren-radio-group>
      </el-form-item>
      <el-form-item label="电话" prop="mobile">
        <el-input v-model="dataForm.mobile" placeholder="电话"></el-input>
      </el-form-item>
      <el-form-item label="车辆类型：" prop="vehicleType">
        <el-radio-group v-model="dataForm.vehicleType">
          <el-radio  label="0">机动车</el-radio>
          <el-radio label="1">非机动车</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";
const { t } = useI18n();
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  id: "",
  houseId: 0,
  houseNum: "",
  realName: "",
  sex: "",
  personType: "0",
  isMaster: "",
  relationship: "",
  mobile: "",
  vehicleType: "",
  remark: "",
});

const rules = ref({
  realName: [{ required: true, message: t("validate.required"), trigger: "change" }],
  personType: [{ required: true, message: t("validate.required"), trigger: "change" }],
  isMaster: [{ required: true, message: t("validate.required"), trigger: "change" }],
  relationship: [{ required: true, message: t("validate.required"), trigger: "change" }],
});

const isMasterChange = (e: any) => {
  if(e){
    dataForm.relationship="本人";
  } else {
    dataForm.relationship="";
  }
};

const init = (id?: number, houseId?: any, houseNum?: any) => {
  visible.value = true;
  dataForm.id = "";
  dataForm.houseId = houseId;
  dataForm.houseNum = houseNum;

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
    getInfo(id);
  }
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/clcHousePersonInfo/clchousepersoninfo/" + id).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/clcHousePersonInfo/clchousepersoninfo", dataForm).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
